package org.example.serialize;

import org.example.spi.SpiLoader;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author Jiangxiaonan
 * @Description
 * @Date
 **/
public class SerializerFactory {
    /**
     * 序列化映射（用于实现单例）
     */
//    private static final Map<String, Serializer> KEY_SERIALIZER_MAP = new HashMap<String, Serializer>()
//    {{
//        put(SerializerKeys.JDK, new JdkSerializer());
//        put(SerializerKeys.KRYO, new KryoSerializer());
//        put(SerializerKeys.HESSIAN, new HessianSerializer());
//        put(SerializerKeys.JSON, new JsonSerializer());
//    }};
    static {
        SpiLoader.load(Serializer.class);
    }

    // 默认序列化器
//    private static final String DEFAULT_SERIALIZER = SerializerKeys.JDK;
    private static final Serializer DEFAULT_SERIALIZER = new JdkSerializer();

    /**
     * 获取序列化器
     *
     */
    public static Serializer getInstance(String serializerKey) {
//        return KEY_SERIALIZER_MAP.getOrDefault(serializerKey, KEY_SERIALIZER_MAP.get(DEFAULT_SERIALIZER));
        return SpiLoader.getInstance(Serializer.class, serializerKey);
    }
}
